<template>
  <view>
    <select-tree :checkList="checkList" v-if="tree.length>0" :options="prop" @sendValue="confirm" :isCheck="false"
      :treeNone="tree"></select-tree>
  </view>
</template>

<script setup lang="ts">
  import SelectTree from '@/components/select-tree-zhikuany/tree.vue'
  import { ref } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import { queryCompanyAndDepart, querySubDepartTree } from '@/services/system'

  const checkList = ref([])
  const tree : any = ref([])
  const prop : any = ref()
  const query = defineProps<{ data : string }>()
  let queryData : any = null
  onLoad(() => {
    if (query.data) {
      queryData = JSON.parse(query.data)
    }
    prop.value = JSON.parse('{"label":"name","children":"children","multiple":false,"nodes":false}')
    uni.showLoading({
      title: ''
    })
    // queryAllTree()
    querySubTree()
    console.log(tree.value)
  })
  //查询组织架构
  const queryAllTree = () => {
    queryCompanyAndDepart().then((res : any) => {
      console.log(res)
      uni.hideLoading()
      if (res.length > 0) {
        const data = res[0]

        setTreeDataAll(data.chileOrgDepartReDomainList, tree.value)
        console.log(tree.value)
      }

    })
  }

  //所有组织
  const setTreeDataAll = (chileOrgDepartReDomainList : any[], tree : any[]) => {
    if (chileOrgDepartReDomainList) {
      //数据转换
      chileOrgDepartReDomainList.forEach((res : any) => {
        const itemDepart : any = {}
        itemDepart.name = res.departName
        itemDepart.id = res.departCode
        itemDepart.user = false
        itemDepart.children = []
        tree.push(itemDepart)
        if (res.chileOrgDepartReDomainList && res.chileOrgDepartReDomainList.length > 0) {//遍历子部门
          setTreeDataAll(res.chileOrgDepartReDomainList, itemDepart.children)
        }
        if (res.orgDepartempReDomainList && res.orgDepartempReDomainList.length > 0) {//人员
          res.orgDepartempReDomainList.forEach((employee : any) => {
            // console.log('employeeName', employee.employeeName)
            itemDepart.children.push({
              user: true,
              name: employee.employeeName,
              id: employee.employeeCode,
            })
          })
        }

      })
    }

  }
  //查询子账号
  const querySubTree = () => {
    querySubDepartTree().then((res : any) => {
      console.log(res)
      uni.hideLoading()
      if (res.length > 0) {

        res.forEach((data : any) => {
          setTreeData(data, tree.value)
        })
        console.log(tree.value)
      }

    })
  }
  //子架构
  const setTreeData = (data : any, tree : any[]) => {

    const itemDepart : any = {}
    itemDepart.name = data.departName
    itemDepart.id = data.departCode
    itemDepart.user = false
    itemDepart.children = []
    tree.push(itemDepart)
    if (data.orgDepartempReDomainList && data.orgDepartempReDomainList.length > 0) {//人员
      data.orgDepartempReDomainList.forEach((employee : any) => {
        // console.log('employeeName', employee.employeeName)
        itemDepart.children.push({
          user: true,
          name: employee.employeeName,
          id: employee.employeeCode,
        })
      })
    }
    if (data.chileOrgDepartReDomainList && data.chileOrgDepartReDomainList.length > 0) {
      //数据转换
      data.chileOrgDepartReDomainList.forEach((subData : any) => {
        setTreeData(subData, itemDepart.children)
      })

    }

  }

  //获取选中的值
  const confirm = (val : any, back : any) => {

    console.log(val[0].user)
    if (!val[0].user) {
      return
    }
    if (queryData.fullDate) {//日报
      uni.navigateTo({
        url: `/pagesMember/workScan/scanFeedbackDetail?data=${JSON.stringify({
          employeeName: val[0].name,
          employeeCode: val[0].id,
          fullDate: queryData.fullDate
        })}`
      })
    } else {//周计划
      uni.navigateTo({
        url: `/pagesMember/workScan/scanPlanDetail?data=${JSON.stringify({
          employeeName: val[0].name,
          employeeCode: val[0].id,
          beginDate: queryData.beginDate,
          endDate: queryData.endDate,
        })}`
      })
    }
  }
</script>

<style>

</style>
